
package com.zjdiepu.www.exception;

import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zjdiepu.www.common.consts.SystemConsts;
/**
 * 项目名称 dpqc-parent-manager-base <br/> 
 * 类名称 MyJsonView <br/>  
 * 类描述 返回json数据<br/>    
 * 创建人 xiongxiaotun <br/> 
 * 创建时间 2017年4月9日 下午4:14:30  <br/>   
 * 修改人 xiongxiaotun <br/> 
 * 修改时间 2017年4月9日 下午4:14:30   <br/>  
 * 修改备注   <br/> 
 * @version V1.0   <br/>
 */
public class MyJsonView extends AbstractView {
	
	public static final String  DEFAULT_CONTENT_TYPE = "application/json;charset=UTF-8";
	
	public final static String  CHARSET                 = "UTF-8";

	private SerializerFeature[] features = new SerializerFeature[] {SerializerFeature.PrettyFormat};
	
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if(!response.isCommitted()) {
			response.reset();
			Object result = model.get(SystemConsts.ERROR_OBJECT);
			response.setContentType(DEFAULT_CONTENT_TYPE);
			response.setCharacterEncoding(CHARSET);
			OutputStream out = response.getOutputStream();
			String text = JSON.toJSONString(result, features);
			byte[] bytes = text.getBytes(CHARSET);
			out.write(bytes);
			out.flush();
		}
	}
}
